package com.pkdl.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.pkdl.dto.BenhNhanDTO;

// Data access object, which get data of BenhNhan table from database
public class BenhNhanDAO {
	public boolean delete(int mabenhnhan) {
		boolean result = true;
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_BenhNhan_Delete(?)}");
			cs.setInt(1, mabenhnhan);
			int n = cs.executeUpdate();
			if (n == 0) {
				result = false;
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(BenhNhanDAO.class.getName()).log(Level.SEVERE,
					null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(BenhNhanDAO.class.getName()).log(Level.SEVERE,
						null, ex);
			}
		}
		return result;
	}

	public boolean insert(BenhNhanDTO benhnhan) {
		boolean result = true;
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_BenhNhan_Insert(?, ?, ?, ?, ?, ?)}");
			cs.setString(1, benhnhan.getHoTen());
			cs.setDate(2, new java.sql.Date(benhnhan.getNgaySinh().getTime()));
			cs.setBoolean(3, benhnhan.getGioiTinh());
			cs.setString(4, benhnhan.getNgheNghiep());
			cs.setString(5, benhnhan.getDiaChi());
			cs.setString(6, benhnhan.getDienThoai());
			int n = cs.executeUpdate();
			if (n == 0) {
				result = false;
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(BenhNhanDAO.class.getName()).log(Level.SEVERE,
					null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(BenhNhanDAO.class.getName()).log(Level.SEVERE,
						null, ex);
			}
		}
		return result;
	}

	public ArrayList<BenhNhanDTO> selectAll() {
		ArrayList<BenhNhanDTO> list = new ArrayList<BenhNhanDTO>();
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_BenhNhan_SelectAll()}");
			ResultSet rs = cs.executeQuery();
			while (rs.next()) {
				BenhNhanDTO benhnhan = new BenhNhanDTO();
				benhnhan.setMaBenhNhan(rs.getInt("MaBenhNhan"));
				benhnhan.setHoTen(rs.getString("HoTen"));
				benhnhan.setNgaySinh(rs.getDate("NgaySinh"));
				benhnhan.setGioiTinh(rs.getBoolean("GioiTinh"));
				benhnhan.setNgheNghiep(rs.getString("NgheNghiep"));
				benhnhan.setDiaChi(rs.getString("DiaChi"));
				benhnhan.setDienThoai(rs.getString("DienThoai"));
				list.add(benhnhan);
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(BenhNhanDAO.class.getName()).log(Level.SEVERE,
					null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(BenhNhanDAO.class.getName()).log(Level.SEVERE,
						null, ex);
			}
		}
		return list;
	}

	public BenhNhanDTO selectById(int mabenhnhan) {
		BenhNhanDTO benhnhan = null;
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_BenhNhan_SelectByID(?)}");
			cs.setInt(1, mabenhnhan);
			ResultSet rs = cs.executeQuery();
			while (rs.next()) {
				benhnhan = new BenhNhanDTO();
				benhnhan.setMaBenhNhan(rs.getInt("MaBenhNhan"));
				benhnhan.setHoTen(rs.getString("HoTen"));
				benhnhan.setNgaySinh(rs.getDate("NgaySinh"));
				benhnhan.setGioiTinh(rs.getBoolean("GioiTinh"));
				benhnhan.setNgheNghiep(rs.getString("NgheNghiep"));
				benhnhan.setDiaChi(rs.getString("DiaChi"));
				benhnhan.setDienThoai(rs.getString("DienThoai"));
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(BenhNhanDAO.class.getName()).log(Level.SEVERE,
					null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(BenhNhanDAO.class.getName()).log(Level.SEVERE,
						null, ex);
			}
		}
		return benhnhan;
	}

	public boolean update(BenhNhanDTO benhnhan) {
		boolean result = true;
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect
					.prepareCall("{call usp_BenhNhan_Update(?, ?, ?, ?, ?, ?, ?)}");
			cs.setInt(1, benhnhan.getMaBenhNhan());
			cs.setString(2, benhnhan.getHoTen());
			cs.setDate(3, new java.sql.Date(benhnhan.getNgaySinh().getTime()));
			cs.setBoolean(4, benhnhan.getGioiTinh());
			cs.setString(5, benhnhan.getNgheNghiep());
			cs.setString(6, benhnhan.getDiaChi());
			cs.setString(7, benhnhan.getDienThoai());
			int n = cs.executeUpdate();
			if (n == 0) {
				result = false;
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(BenhNhanDAO.class.getName()).log(Level.SEVERE,
					null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(BenhNhanDAO.class.getName()).log(Level.SEVERE,
						null, ex);
			}
		}
		return result;
	}

}
